/*!
 * Copyright (c) 2010 Jacob Essex
 *
 * Dual licenced under MIT or GPL 3 licneces
 * 
 * See the GPL3 or MIT files in the main directory for details
 */

require.def('db/answers', ['db'],
function(Db){
    function QueuedAnswer(db, tbl, id){
        this.__proto__ = new Db.Row(this, db, tbl, id);

        this.Added    = 0;
        this.SeQuestionId = 0;
        this.SeAnswerId = 0;
        this.UpVotes = 0;
        this.DownVotes = 0;
    }
    return{
        /**
         * This database is to get around the fact that if the user want to include their entire list of answers in their dataset,
         * they could quite possibly hit the 10k limit fetching the data. The way to get around is to generate the list of questions
         * to fetch into a queue and then take them off one at a time.
         */ 
        QueuedAnswersDb:function(db){
            var tbl = new Db.TableDef('QueuedAnswers', ['SeQuestionId','SeAnswerId', 'Added', 'UpVotes', 'DownVotes']);

            this.destroy = function(tx, funcs){
                var execSql = function(tx){
                    tx.executeSql(
                        'DROP TABLE IF EXISTS ' + tbl.name,
                        [],
                        funcs && funcs.success ? function(tx,rs){funcs.success(tx);} : function(){},
                        funcs && funcs.error ? function(tx, e){funcs.error(e.message);} : function(){}
                    );
                };
                if ( tx ){
                    execSql(tx);
                }else{
                    db.transaction(execSql);
                }
            };

            this.create = function(tx, funcs){
                var execSql = function(tx){
                    var sql = 'CREATE TABLE IF NOT EXISTS ' + tbl.name + ' ('
                            + 'Id integer, SeQuestionId integer, Added integer, '
                            + 'UpVotes integer, DownVotes integer,'
                            + 'SeAnswerId integer,'
                            + 'PRIMARY KEY(Id)'
                            + ')';
                    tx.executeSql(
                        sql,
                        [],
                        funcs && funcs.success ? function(tx,rs){funcs.success();} : function(){},
                        funcs && funcs.error ? function(tx, e){funcs.error(e.message);} : function(){}
                    );
                }
                if ( tx ){
                    execSql(tx);
                }else{
                    db.transaction(execSql);
                }
            }
            
            this.createAnswer = function(tx, funcs){
                //this line cannot be moved into the fucntion call arugments
                //to avoid the temp object as the function isn't guarenteed to be called
                var qa = new QueuedAnswer(db, tbl, Db.ROW_NOT_CREATED);
                funcs && funcs.success && funcs.success(tx, qa);
            }


            this.seAnswerExists = function(tx, id, funcs){
                var execSql = function(tx){
                    Db.site.exists(tx, tbl.name, ['SeAnswerId'],[id], funcs);
                };
                if ( tx ){
                    execSql(tx);
                }else{
                    db.transaction(execSql);
                }
            }
            /**
             * Note, this returns an array to the success function
             */
            this.getByQuestionId = function(tx, id, funcs){
                if ( funcs === undefined ){
                    return;
                }

                var execSql = function(tx){
                    var sql = 'SELECT * FROM ' + tbl.name + ' WHERE SeQuestionId = ? ';
                    tx.executeSql(
                            sql,
                            [id],
                            function(tx, rs){
                                var items = [];
                                //TODO fix copy and paste
                                for ( var i = 0; i < rs.rows.length; i++ ){
                                    var row = rs.rows.item(i);
                                    var id = row['Id'];
                                    var obj = new QueuedAnswer(db, tbl, id); 
                                    tbl.columns.forEach(function(k){ obj[k] = row[k]; }); //transfer properties
                                    items.push(obj);
                                }
                                funcs.success && funcs.success(tx, items);
                            },
                            function(tx, err){
                                funcs.error && funcs.error(err.message);
                            }
                    );
                };

                if ( tx ){
                    execSql(tx);
                }else{
                    db.transaction(execSql);
                }
            }

            this.getAnswers = function(tx, funcs){
                if ( funcs === undefined ){
                    return;
                }
                var execSql = function(tx){
                    var sql = 'SELECT * FROM ' + tbl.name + ' WHERE Added = 0';
                    tx.executeSql(
                            sql,
                            [],
                            function(tx, rs){
                                var items = [];
                                //copy and paste with above
                                for ( var i = 0; i < rs.rows.length; i++ ){
                                    var row = rs.rows.item(i);
                                    var id = row['Id'];
                                    var obj = new QueuedAnswer(db, tbl, id); 
                                    tbl.columns.forEach(function(k){ obj[k] = row[k]; }); //transfer properties
                                    items.push(obj);
                                }
                                funcs.success && funcs.success(tx, items);
                            },
                            function(tx, err){
                                funcs.error && funcs.error(err.message);
                            }
                    );
                };
                if ( tx ){
                    execSql(tx);
                }else{
                    db.transaction(execSql);
                }
            }

            this.count = function(tx, funcs){
                var execSql = function(tx){
                    Db.site.count(tx, tbl.name, [], funcs);
                }
                if ( tx ){
                    execSql(tx);
                }else{
                    db.transaction(execSql);
                }
            }

        } 
    }
});
